package com.tuyue.police.api;

import com.tuyue.police.bean.UrlBean;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.redis.RedisService;
import com.tuyue.police.response.AccessTokenResponse;
import com.tuyue.police.util.HttpUtils;
import com.tuyue.police.util.JsonToObject;
import com.tuyue.police.util.Result;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 王金海
 * @Description: 获取AccessToken
 * @Date: Created by Administrator on 2018/6/1.
 * @Modified By:
 */
@Component
public class AccessTokenApi {
    private static final String ACCESSTOKEN="AccessToken";
    @Value("${appKey}")
    private String appKey;
    @Value("${appSecret}")
    private String appSecret;

    @Autowired
    private RedisService<String> redisService;

    /**
     * 获取大用户AccessToken
     * @return
     */
    public String getAccessToken(){
        String token = redisService.get(ACCESSTOKEN);
        if (StringUtils.isEmpty(token)) {
            token = getAccessTokenApi();
        }

        return token;

    }

    /**
     * API获取AccessToken
     * @return
     */
    private String getAccessTokenApi(){
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("appKey", appKey);
        querys.put("appSecret", appSecret);
        return getString(UrlBean.GETACCESSTOKEN,ACCESSTOKEN, querys);
    }

    /**
     * 获取自子用户Token
     * @param accountId
     * @return
     */
    public String getUserAccessToken(String accountId){
        String userAccessToken = redisService.get(ACCESSTOKEN + accountId);
        if (!StringUtils.isEmpty(userAccessToken)) {
            return userAccessToken;
        }
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("accessToken", getAccessToken());
        querys.put("accountId", accountId);
        return getString(UrlBean.GETRAMACCESSTOKEN,accountId, querys);
    }

    private String getString(String url,String key, Map<String, String> querys) {
        HttpResponse response;
        try {
            response = HttpUtils.doPost( url, querys,null);
            String s = EntityUtils.toString(response.getEntity());
            Result<AccessTokenResponse> result = JsonToObject.parseObject(s, AccessTokenResponse.class);
            if (result.getCode()!=200) {
                throw new MyException(result.getCode(),result.getMsg());
            }
            String accessToken = result.getData().getAccessToken();
            Long expireTime = result.getData().getExpireTime();
            redisService.set(key,accessToken,expireTime);
            return accessToken;
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(ResultEnum.YS_SERVER_ERROR);
        }
    }


}
